/**
 * 仿写JSON内置API的parse方法
 * @param String
 * @returns Object
 */
function parse(user){
    //  let user = '{"id":1, "username":"阿伟" , "password":"fuckYou"}';
    //1去空格 " 大括号
    let strDelete = user.replace(/[{}"\s]/g,"")//username:阿伟,password:fuckYou
    // console.log(strDelete);
    //2以[,]分隔
    let arr = strDelete.split(',')//['username:阿伟', 'password:fuckYou']
    // console.log(arr);
    //3定义空对象
    let obj = {}
    //4遍历
    arr.forEach(item => {
        //5遍历里面:按:分割
        let temp = item.split(':')
        //6temp[0]是键 temp[1]是值
        // obj[temp[0]] = temp[1]//当对象的键是变量时，用数组语法
    obj[temp[0]]=isNaN(Number(temp[1])) ? temp[1] : Number(temp[1])
    })
    // console.log(obj);
    return obj
}
// parse('{"id":1, "username":"阿伟" , "password":"fuckYou"}')

/**
 * 仿写JSON内置API的stringify方法
 * @param Object 
 * @returns String
 */
 let obj = {"id":1, "username":"阿伟" , "password":"fuckYou" ,"status":true};
 let arr= []
 for (let key in obj){
     let val = obj[key]
     if(!['boolean','number'].includes(typeof val)){
         val = `"${obj[key]}"`
     }
     arr.push(`"${key}":${val}`)
 }
 console.log(`{${arr.join(',')}}`);

